package abq2.jic.jic.plugin.operate.jianli;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.ext.form.control.Markdown;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

import java.text.SimpleDateFormat;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;
/**
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */

/**
 * 用于智能总结日任务情况
 */
public class GetQuestion extends AbstractBillPlugIn {
    @Override
    public void registerListener(EventObject e) {
        //注册点击事件
        super.registerListener(e);
        this.addItemClickListeners("abq2_toolbarap");
    }

    public void itemClick(ItemClickEvent e) {
        super.itemClick(e);
        if (e.getItemKey().equalsIgnoreCase("abq2_question_button")) {
            long userId = UserServiceHelper.getCurrentUserId();
            //获取DynamicObject列表，即获取学生的单据头的信息
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_employment_apply",
                    "abq2_creator," + //创建人
                            "abq2_createtime," +    //创建时间
                            "abq2_combofield1," +          //性别
                            "abq2_textfield2," +            //专业
                            "abq2_textfield3," +     //毕业院校
                            "abq2_largetextfield7," +      //求职意向
                            "abq2_largetextfield8," +   //专业课程
                            "abq2_largetextfield," +     //实习经历
                            "abq2_largetextfield1," + //校园经历
                            "abq2_largetextfield2," +   //个人技能
                            "abq2_largetextfield5," +   //获奖经历
                            "abq2_largetextfield3" +   //兴趣爱好
                            "abq2_largetextfield4",            //个人评价
                    (new QFilter("abq2_creator", QCP.equals, userId)).toArray());


            JSONArray jsonArray = new JSONArray();
            JSONObject jsonResultObject = new JSONObject();
            for (DynamicObject dy : dys) {

                jsonResultObject.put("createTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dy.getDate("abq2_createtime")));
                jsonResultObject.put("gender", dy.getString("abq2_combofield1"));
                jsonResultObject.put("major", dy.getString("abq2_textfield2"));
                jsonResultObject.put("graduation", dy.getString("abq2_textfield3"));
                jsonResultObject.put("jobIntention", dy.getString("abq2_largetextfield7"));
                jsonResultObject.put("course", dy.getString("abq2_largetextfield8"));
                jsonResultObject.put("workExperience", dy.getString("abq2_largetextfield"));
                jsonResultObject.put("schoolExperience", dy.getString("abq2_largetextfield1"));
                jsonResultObject.put("selfAbility", dy.getString("abq2_largetextfield2"));
                jsonResultObject.put("award", dy.getString("abq2_largetextfield5"));
//                jsonResultObject.put("interest", dy.getString("abq2_largetextfield3"));
//                jsonResultObject.put("PersonalEvaluation", dy.getString("abq2_largetextfield4"));

                jsonArray.add(jsonResultObject);
                break;
            }

            //获取日任务信息，并且以JSON字符串的形式展现




            //调用GPT开发平台微服务
            Map<String , String> variableMap = new HashMap<>();
            variableMap.put("taskResult", jsonArray.toJSONString());

            Object[] params = new Object[] {
                    //GPT提示编码
                    getPromptFid("prompt-2405288483AA93"),
                    "",
                    variableMap
            };
            Map<String, Object> result = DispatchServiceHelper.invokeBizService("ai", "gai", "GaiPromptService", "syncCall", params);
            JSONObject jsonObjectResult = new JSONObject(result);
            JSONObject jsonObjectData = jsonObjectResult.getJSONObject("data");
            //设置值
            this.getModel().setValue("abq2_question", jsonObjectData.getString("llmValue"));
            Markdown mk = this.getView().getControl("abq2_markdownap_question");
            mk.setText(jsonObjectData.getString("llmValue"));
        }

    }

    //获取GPT提示的Fid
    public long getPromptFid(String billNo) {
        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("gai_prompt",
                "number," +
                        "id",
                (new QFilter("number", QCP.equals, billNo)).toArray());
        return dynamicObject.getLong("id");
    }

    @Override
    public void afterBindData(EventObject eventObject) {
        Markdown mk = this.getView().getControl("abq2_markdownap_question");
        mk.setText(this.getModel().getValue("abq2_question").toString());
    }
}